<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chat Interface</title>
    <style>
        body {
            font-family: 'Helvetica Neue', Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f0f0f0;
        }

        #chat-container {
            max-width: 600px;
            margin: 0 auto;
            background-color: #ffffff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        #chat-input {
            width: calc(100% - 100px);
            padding: 15px;
            font-size: 16px;
            border-radius: 5px;
            border: 1px solid #ccc;
            margin-top: 10px;
            float: left;
        }

        #send-button {
            width: 80px;
            padding: 15px;
            font-size: 16px;
            border-radius: 5px;
            border: 1px solid #ccc;
            margin-top: 10px;
            background-color: #007bff;
            color: white;
            cursor: pointer;
            float: right;
        }

        #send-button:hover {
            background-color: #0056b3;
        }

        #chat-messages {
            margin-top: 20px;
            height: 300px;
            overflow-y: auto;
            padding: 10px;
            background-color: #ffffff;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .message {
            margin-bottom: 10px;
            padding: 10px;
            border-radius: 5px;
            background-color: #e0e0e0;
            position: relative;
            padding-left: 50px;
        }

        .message:before {
            content: '';
            position: absolute;
            top: 0;
            left: 10px;
            bottom: 0;
            width: 30px;
            height: 30px;
            background-color: #d0d0d0;
            border-radius: 50%;
        }

        .message.mine {
            background-color: #90caf9;
            align-self: flex-end;
        }

        .message.mine:before {
            left: auto;
            right: 10px;
            background-color: #64b5f6;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
            }

            to {
                opacity: 1;
            }
        }

        .message {
            animation: fadeIn 0.5s ease-in;
        }
    </style>
</head>

<body>
    <div id="chat-container">
        <h2>Chat with AI</h2>
        <input type="text" id="chat-input" placeholder="Enter your message...">
        <button id="send-button" onclick="sendMessage()">Send</button>
        <div id="chat-messages"></div>
    </div>
    <script>
        function sendMessage() {
            const input = document.getElementById('chat-input');
            const messagesContainer = document.getElementById('chat-messages');
            const messageText = input.value;
            if (messageText.trim() === '') {
                alert('Please enter a message.');
                return;
            }
            // Send message to the server
            fetch('http://localhost:8080/chat', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Origin': 'http://localhost:8081',
                },
                body: JSON.stringify({ text: messageText }),
            })
                .then(response => response.json())
                .then(data => {
                    if (data.code === 0 && data.msg === 'ok') {
                        const messageElement = document.createElement('div');
                        messageElement.classList.add('message');
                        messageElement.innerText = data.data;
                        messagesContainer.appendChild(messageElement);
                        messagesContainer.scrollTop = messagesContainer.scrollHeight;
                    } else {
                        alert('Error: ' + data.msg);
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('An error occurred while sending the message.');
                });
            input.value = '';
        }
    </script>
</body>

</html>